{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2241,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import datetime\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取数据 5分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2242,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>申请者Id</th>\n",
       "      <th>账户号</th>\n",
       "      <th>是否违约Y</th>\n",
       "      <th>汽车购买时间</th>\n",
       "      <th>汽车制造商</th>\n",
       "      <th>是否曾经破产</th>\n",
       "      <th>五年内信用不良事件数量</th>\n",
       "      <th>账户数量</th>\n",
       "      <th>最久账户存续时间(月)</th>\n",
       "      <th>在使用账户数量</th>\n",
       "      <th>...</th>\n",
       "      <th>汽车购买金额(元)</th>\n",
       "      <th>建议售价</th>\n",
       "      <th>分期付款的首次交款</th>\n",
       "      <th>贷款期限(月)</th>\n",
       "      <th>贷款金额</th>\n",
       "      <th>贷款金额/建议售价*100</th>\n",
       "      <th>月均收入(元)</th>\n",
       "      <th>行使历程(Mile)</th>\n",
       "      <th>是否二手车</th>\n",
       "      <th>样本权重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2314049</td>\n",
       "      <td>11613</td>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>FORD</td>\n",
       "      <td>N</td>\n",
       "      <td>7.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>17350.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>36</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>99.0</td>\n",
       "      <td>6550.00</td>\n",
       "      <td>24000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>63539</td>\n",
       "      <td>13449</td>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>DAEWOO</td>\n",
       "      <td>N</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>...</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>19788.0</td>\n",
       "      <td>683.54</td>\n",
       "      <td>60</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>99.0</td>\n",
       "      <td>4666.67</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7328510</td>\n",
       "      <td>14323</td>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>PLYMOUTH</td>\n",
       "      <td>N</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>13595.00</td>\n",
       "      <td>11450.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>60</td>\n",
       "      <td>10500.00</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2000.00</td>\n",
       "      <td>19600.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8725187</td>\n",
       "      <td>15359</td>\n",
       "      <td>1</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>FORD</td>\n",
       "      <td>N</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>12999.00</td>\n",
       "      <td>12100.0</td>\n",
       "      <td>3099.00</td>\n",
       "      <td>60</td>\n",
       "      <td>10800.00</td>\n",
       "      <td>118.0</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4275127</td>\n",
       "      <td>15812</td>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>TOYOTA</td>\n",
       "      <td>N</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>104.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>22024.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>60</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>122.0</td>\n",
       "      <td>4144.00</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     申请者Id    账户号  是否违约Y  汽车购买时间     汽车制造商 是否曾经破产  五年内信用不良事件数量  账户数量  \\\n",
       "0  2314049  11613      1  1998.0      FORD      N          7.0   9.0   \n",
       "1    63539  13449      0  2000.0    DAEWOO      N          0.0  21.0   \n",
       "2  7328510  14323      1  1998.0  PLYMOUTH      N          7.0  10.0   \n",
       "3  8725187  15359      1  1997.0      FORD      N          3.0  10.0   \n",
       "4  4275127  15812      0  2000.0    TOYOTA      N          0.0  10.0   \n",
       "\n",
       "   最久账户存续时间(月)  在使用账户数量  ...  汽车购买金额(元)     建议售价  分期付款的首次交款  贷款期限(月)  \\\n",
       "0         64.0      2.0  ...   17200.00  17350.0       0.00       36   \n",
       "1        240.0     11.0  ...   19588.54  19788.0     683.54       60   \n",
       "2         60.0      NaN  ...   13595.00  11450.0       0.00       60   \n",
       "3         35.0      5.0  ...   12999.00  12100.0    3099.00       60   \n",
       "4        104.0      2.0  ...   26328.04  22024.0       0.00       60   \n",
       "\n",
       "       贷款金额  贷款金额/建议售价*100  月均收入(元)  行使历程(Mile)  是否二手车  样本权重  \n",
       "0  17200.00           99.0  6550.00     24000.0      1  1.00  \n",
       "1  19588.54           99.0  4666.67        22.0      0  4.75  \n",
       "2  10500.00           92.0  2000.00     19600.0      1  1.00  \n",
       "3  10800.00          118.0  1500.00     10000.0      1  1.00  \n",
       "4  26328.04          122.0  4144.00        14.0      0  4.75  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 2242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(r'I:\\拉钩数据分析实训营\\数据分析实战训练营\\9一线互联网数据分析综合项目实战\\1.电商B2C商铺新用户复购预测\\作业\\作业资料下载\\车贷违约.utf8.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2243,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = data.drop(['申请者Id','账户号'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2244,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5845, 23)"
      ]
     },
     "execution_count": 2244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2245,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "是否违约Y                       0\n",
       "汽车购买时间                      1\n",
       "汽车制造商                     299\n",
       "是否曾经破产                    217\n",
       "五年内信用不良事件数量               213\n",
       "账户数量                      213\n",
       "最久账户存续时间(月)               216\n",
       "在使用账户数量                  1419\n",
       "在使用可循环贷款账户数量(比如信用卡)       638\n",
       "在使用可循环贷款帐户余额(比如信用卡欠款)     478\n",
       "可循环贷款帐户限额(信用卡授权额度)        478\n",
       "可循环贷款帐户使用比例(余额/限额)          0\n",
       "FICO打分                    314\n",
       "汽车购买金额(元)                   0\n",
       "建议售价                        1\n",
       "分期付款的首次交款                   0\n",
       "贷款期限(月)                     0\n",
       "贷款金额                        0\n",
       "贷款金额/建议售价*100               1\n",
       "月均收入(元)                     5\n",
       "行使历程(Mile)                  1\n",
       "是否二手车                       0\n",
       "样本权重                        0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 特征筛选 + 异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2246,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>汽车制造商</th>\n",
       "      <th>是否曾经破产</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5546</td>\n",
       "      <td>5628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>154</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>FORD</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>1112</td>\n",
       "      <td>5180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       汽车制造商 是否曾经破产\n",
       "count   5546   5628\n",
       "unique   154      2\n",
       "top     FORD      N\n",
       "freq    1112   5180"
      ]
     },
     "execution_count": 2246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.describe(include='object')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2247,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否违约Y</th>\n",
       "      <th>汽车购买时间</th>\n",
       "      <th>五年内信用不良事件数量</th>\n",
       "      <th>账户数量</th>\n",
       "      <th>最久账户存续时间(月)</th>\n",
       "      <th>在使用账户数量</th>\n",
       "      <th>在使用可循环贷款账户数量(比如信用卡)</th>\n",
       "      <th>在使用可循环贷款帐户余额(比如信用卡欠款)</th>\n",
       "      <th>可循环贷款帐户限额(信用卡授权额度)</th>\n",
       "      <th>可循环贷款帐户使用比例(余额/限额)</th>\n",
       "      <th>...</th>\n",
       "      <th>汽车购买金额(元)</th>\n",
       "      <th>建议售价</th>\n",
       "      <th>分期付款的首次交款</th>\n",
       "      <th>贷款期限(月)</th>\n",
       "      <th>贷款金额</th>\n",
       "      <th>贷款金额/建议售价*100</th>\n",
       "      <th>月均收入(元)</th>\n",
       "      <th>行使历程(Mile)</th>\n",
       "      <th>是否二手车</th>\n",
       "      <th>样本权重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5845.000000</td>\n",
       "      <td>5844.000000</td>\n",
       "      <td>5632.000000</td>\n",
       "      <td>5632.000000</td>\n",
       "      <td>5629.000000</td>\n",
       "      <td>4426.000000</td>\n",
       "      <td>5207.000000</td>\n",
       "      <td>5367.000000</td>\n",
       "      <td>5367.000000</td>\n",
       "      <td>5845.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>5845.000000</td>\n",
       "      <td>5844.000000</td>\n",
       "      <td>5845.000000</td>\n",
       "      <td>5845.000000</td>\n",
       "      <td>5845.000000</td>\n",
       "      <td>5844.00000</td>\n",
       "      <td>5.840000e+03</td>\n",
       "      <td>5844.000000</td>\n",
       "      <td>5845.000000</td>\n",
       "      <td>5845.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.204790</td>\n",
       "      <td>1901.793634</td>\n",
       "      <td>1.910156</td>\n",
       "      <td>17.084695</td>\n",
       "      <td>154.304317</td>\n",
       "      <td>5.720063</td>\n",
       "      <td>3.093336</td>\n",
       "      <td>6218.619899</td>\n",
       "      <td>18262.655674</td>\n",
       "      <td>43.444482</td>\n",
       "      <td>...</td>\n",
       "      <td>19145.235109</td>\n",
       "      <td>18643.180243</td>\n",
       "      <td>1325.375624</td>\n",
       "      <td>56.806159</td>\n",
       "      <td>17660.066222</td>\n",
       "      <td>98.78525</td>\n",
       "      <td>6.206255e+03</td>\n",
       "      <td>20167.981348</td>\n",
       "      <td>0.564756</td>\n",
       "      <td>3.982036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.403583</td>\n",
       "      <td>488.024392</td>\n",
       "      <td>3.274744</td>\n",
       "      <td>10.814056</td>\n",
       "      <td>99.940540</td>\n",
       "      <td>3.165783</td>\n",
       "      <td>2.401923</td>\n",
       "      <td>8657.667616</td>\n",
       "      <td>20942.605070</td>\n",
       "      <td>75.289977</td>\n",
       "      <td>...</td>\n",
       "      <td>9356.070282</td>\n",
       "      <td>10190.495573</td>\n",
       "      <td>2435.177463</td>\n",
       "      <td>14.547659</td>\n",
       "      <td>9095.267595</td>\n",
       "      <td>18.08215</td>\n",
       "      <td>1.073186e+05</td>\n",
       "      <td>29464.181138</td>\n",
       "      <td>0.495831</td>\n",
       "      <td>1.513436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>2133.400000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1997.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>78.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>791.000000</td>\n",
       "      <td>3235.500000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>12684.000000</td>\n",
       "      <td>12050.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>51.000000</td>\n",
       "      <td>11023.000000</td>\n",
       "      <td>90.00000</td>\n",
       "      <td>2.218245e+03</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1999.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>16.000000</td>\n",
       "      <td>137.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3009.000000</td>\n",
       "      <td>10574.000000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>18017.750000</td>\n",
       "      <td>17475.000000</td>\n",
       "      <td>500.000000</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>16200.000000</td>\n",
       "      <td>100.00000</td>\n",
       "      <td>3.400000e+03</td>\n",
       "      <td>8000.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>4.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>2000.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>205.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>8461.500000</td>\n",
       "      <td>26196.000000</td>\n",
       "      <td>66.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>24500.000000</td>\n",
       "      <td>23751.250000</td>\n",
       "      <td>1750.000000</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>22800.000000</td>\n",
       "      <td>109.00000</td>\n",
       "      <td>5.156250e+03</td>\n",
       "      <td>34135.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>4.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>9999.000000</td>\n",
       "      <td>32.000000</td>\n",
       "      <td>77.000000</td>\n",
       "      <td>588.000000</td>\n",
       "      <td>26.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>96260.000000</td>\n",
       "      <td>205395.000000</td>\n",
       "      <td>2500.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>111554.000000</td>\n",
       "      <td>222415.000000</td>\n",
       "      <td>35000.000000</td>\n",
       "      <td>660.000000</td>\n",
       "      <td>111554.000000</td>\n",
       "      <td>176.00000</td>\n",
       "      <td>8.147167e+06</td>\n",
       "      <td>999999.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>4.750000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             是否违约Y       汽车购买时间  五年内信用不良事件数量         账户数量  最久账户存续时间(月)  \\\n",
       "count  5845.000000  5844.000000  5632.000000  5632.000000  5629.000000   \n",
       "mean      0.204790  1901.793634     1.910156    17.084695   154.304317   \n",
       "std       0.403583   488.024392     3.274744    10.814056    99.940540   \n",
       "min       0.000000     0.000000     0.000000     0.000000     1.000000   \n",
       "25%       0.000000  1997.000000     0.000000     9.000000    78.000000   \n",
       "50%       0.000000  1999.000000     0.000000    16.000000   137.000000   \n",
       "75%       0.000000  2000.000000     2.000000    24.000000   205.000000   \n",
       "max       1.000000  9999.000000    32.000000    77.000000   588.000000   \n",
       "\n",
       "           在使用账户数量  在使用可循环贷款账户数量(比如信用卡)  在使用可循环贷款帐户余额(比如信用卡欠款)  \\\n",
       "count  4426.000000          5207.000000            5367.000000   \n",
       "mean      5.720063             3.093336            6218.619899   \n",
       "std       3.165783             2.401923            8657.667616   \n",
       "min       0.000000             0.000000               0.000000   \n",
       "25%       3.000000             1.000000             791.000000   \n",
       "50%       5.000000             3.000000            3009.000000   \n",
       "75%       7.000000             4.000000            8461.500000   \n",
       "max      26.000000            24.000000           96260.000000   \n",
       "\n",
       "       可循环贷款帐户限额(信用卡授权额度)  可循环贷款帐户使用比例(余额/限额)  ...      汽车购买金额(元)  \\\n",
       "count         5367.000000         5845.000000  ...    5845.000000   \n",
       "mean         18262.655674           43.444482  ...   19145.235109   \n",
       "std          20942.605070           75.289977  ...    9356.070282   \n",
       "min              0.000000            0.000000  ...       0.000000   \n",
       "25%           3235.500000            5.000000  ...   12684.000000   \n",
       "50%          10574.000000           30.000000  ...   18017.750000   \n",
       "75%          26196.000000           66.000000  ...   24500.000000   \n",
       "max         205395.000000         2500.000000  ...  111554.000000   \n",
       "\n",
       "                建议售价     分期付款的首次交款      贷款期限(月)           贷款金额  贷款金额/建议售价*100  \\\n",
       "count    5844.000000   5845.000000  5845.000000    5845.000000     5844.00000   \n",
       "mean    18643.180243   1325.375624    56.806159   17660.066222       98.78525   \n",
       "std     10190.495573   2435.177463    14.547659    9095.267595       18.08215   \n",
       "min         0.000000      0.000000    12.000000    2133.400000        0.00000   \n",
       "25%     12050.000000      0.000000    51.000000   11023.000000       90.00000   \n",
       "50%     17475.000000    500.000000    60.000000   16200.000000      100.00000   \n",
       "75%     23751.250000   1750.000000    60.000000   22800.000000      109.00000   \n",
       "max    222415.000000  35000.000000   660.000000  111554.000000      176.00000   \n",
       "\n",
       "            月均收入(元)     行使历程(Mile)        是否二手车         样本权重  \n",
       "count  5.840000e+03    5844.000000  5845.000000  5845.000000  \n",
       "mean   6.206255e+03   20167.981348     0.564756     3.982036  \n",
       "std    1.073186e+05   29464.181138     0.495831     1.513436  \n",
       "min    0.000000e+00       0.000000     0.000000     1.000000  \n",
       "25%    2.218245e+03       1.000000     0.000000     4.750000  \n",
       "50%    3.400000e+03    8000.000000     1.000000     4.750000  \n",
       "75%    5.156250e+03   34135.500000     1.000000     4.750000  \n",
       "max    8.147167e+06  999999.000000     1.000000     4.750000  \n",
       "\n",
       "[8 rows x 21 columns]"
      ]
     },
     "execution_count": 2247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2248,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5845 entries, 0 to 5844\n",
      "Data columns (total 23 columns):\n",
      " #   Column                 Non-Null Count  Dtype  \n",
      "---  ------                 --------------  -----  \n",
      " 0   是否违约Y                  5845 non-null   int64  \n",
      " 1   汽车购买时间                 5844 non-null   float64\n",
      " 2   汽车制造商                  5546 non-null   object \n",
      " 3   是否曾经破产                 5628 non-null   object \n",
      " 4   五年内信用不良事件数量            5632 non-null   float64\n",
      " 5   账户数量                   5632 non-null   float64\n",
      " 6   最久账户存续时间(月)            5629 non-null   float64\n",
      " 7   在使用账户数量                4426 non-null   float64\n",
      " 8   在使用可循环贷款账户数量(比如信用卡)    5207 non-null   float64\n",
      " 9   在使用可循环贷款帐户余额(比如信用卡欠款)  5367 non-null   float64\n",
      " 10  可循环贷款帐户限额(信用卡授权额度)     5367 non-null   float64\n",
      " 11  可循环贷款帐户使用比例(余额/限额)     5845 non-null   int64  \n",
      " 12  FICO打分                 5531 non-null   float64\n",
      " 13  汽车购买金额(元)              5845 non-null   float64\n",
      " 14  建议售价                   5844 non-null   float64\n",
      " 15  分期付款的首次交款              5845 non-null   float64\n",
      " 16  贷款期限(月)                5845 non-null   int64  \n",
      " 17  贷款金额                   5845 non-null   float64\n",
      " 18  贷款金额/建议售价*100          5844 non-null   float64\n",
      " 19  月均收入(元)                5840 non-null   float64\n",
      " 20  行使历程(Mile)             5844 non-null   float64\n",
      " 21  是否二手车                  5845 non-null   int64  \n",
      " 22  样本权重                   5845 non-null   float64\n",
      "dtypes: float64(17), int64(4), object(2)\n",
      "memory usage: 1.0+ MB\n"
     ]
    }
   ],
   "source": [
    "data2.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2249,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>汽车购买时间</th>\n",
       "      <th>0.0</th>\n",
       "      <th>1977.0</th>\n",
       "      <th>1982.0</th>\n",
       "      <th>1985.0</th>\n",
       "      <th>1986.0</th>\n",
       "      <th>1988.0</th>\n",
       "      <th>1989.0</th>\n",
       "      <th>1990.0</th>\n",
       "      <th>1991.0</th>\n",
       "      <th>1992.0</th>\n",
       "      <th>1993.0</th>\n",
       "      <th>1994.0</th>\n",
       "      <th>1995.0</th>\n",
       "      <th>1996.0</th>\n",
       "      <th>1997.0</th>\n",
       "      <th>1998.0</th>\n",
       "      <th>1999.0</th>\n",
       "      <th>2000.0</th>\n",
       "      <th>2001.0</th>\n",
       "      <th>9999.0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>是否违约Y</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>236</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>18</td>\n",
       "      <td>24</td>\n",
       "      <td>61</td>\n",
       "      <td>122</td>\n",
       "      <td>201</td>\n",
       "      <td>346</td>\n",
       "      <td>563</td>\n",
       "      <td>512</td>\n",
       "      <td>825</td>\n",
       "      <td>1718</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>18</td>\n",
       "      <td>48</td>\n",
       "      <td>71</td>\n",
       "      <td>108</td>\n",
       "      <td>150</td>\n",
       "      <td>141</td>\n",
       "      <td>220</td>\n",
       "      <td>365</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "汽车购买时间  0.0     1977.0  1982.0  1985.0  1986.0  1988.0  1989.0  1990.0  \\\n",
       "是否违约Y                                                                    \n",
       "0          236       1       0       1       1       1       2      11   \n",
       "1           62       0       1       0       1       0       1       1   \n",
       "\n",
       "汽车购买时间  1991.0  1992.0  1993.0  1994.0  1995.0  1996.0  1997.0  1998.0  \\\n",
       "是否违约Y                                                                    \n",
       "0           18      24      61     122     201     346     563     512   \n",
       "1            1       8      18      48      71     108     150     141   \n",
       "\n",
       "汽车购买时间  1999.0  2000.0  2001.0  9999.0  \n",
       "是否违约Y                                   \n",
       "0          825    1718       1       3  \n",
       "1          220     365       0       1  "
      ]
     },
     "execution_count": 2249,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cross_tab = pd.crosstab(data2.是否违约Y,data2.汽车购买时间)\n",
    "cross_tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2250,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(37.4400457088725,\n",
       " 0.006987182756537592,\n",
       " 19,\n",
       " array([[2.36962012e+02, 7.95174538e-01, 7.95174538e-01, 7.95174538e-01,\n",
       "         1.59034908e+00, 7.95174538e-01, 2.38552361e+00, 9.54209446e+00,\n",
       "         1.51083162e+01, 2.54455852e+01, 6.28187885e+01, 1.35179671e+02,\n",
       "         2.16287474e+02, 3.61009240e+02, 5.66959446e+02, 5.19248973e+02,\n",
       "         8.30957392e+02, 1.65634856e+03, 7.95174538e-01, 3.18069815e+00],\n",
       "        [6.10379877e+01, 2.04825462e-01, 2.04825462e-01, 2.04825462e-01,\n",
       "         4.09650924e-01, 2.04825462e-01, 6.14476386e-01, 2.45790554e+00,\n",
       "         3.89168378e+00, 6.55441478e+00, 1.61812115e+01, 3.48203285e+01,\n",
       "         5.57125257e+01, 9.29907598e+01, 1.46040554e+02, 1.33751027e+02,\n",
       "         2.14042608e+02, 4.26651437e+02, 2.04825462e-01, 8.19301848e-01]]))"
      ]
     },
     "execution_count": 2250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy import stats\n",
    "data2.汽车购买时间.value_counts()\n",
    "stats.chi2_contingency(cross_tab)  # 将汽车购买时间当错分类变量,使用卡方检验\n",
    "# 返回结果：chi2卡方值，P值，自由程度 alpha=0.05 > P值0.006 说明汽车购买时间对是否违约有影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2251,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['汽车购买时间'] = data2.汽车购买时间.mask(data2.汽车购买时间 < data2.汽车购买时间.quantile(.1),data2.汽车购买时间.quantile(.1))\n",
    "data2['汽车购买时间'] = data2.汽车购买时间.mask(data2.汽车购买时间 > data2.汽车购买时间.quantile(.9),data2.汽车购买时间.quantile(.9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2252,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>汽车制造商</th>\n",
       "      <th>BUICK</th>\n",
       "      <th>CADILLAC</th>\n",
       "      <th>CHEV</th>\n",
       "      <th>CHEVROLET</th>\n",
       "      <th>CHEVY</th>\n",
       "      <th>CHRYSLER</th>\n",
       "      <th>DODGE</th>\n",
       "      <th>FORD</th>\n",
       "      <th>GEO</th>\n",
       "      <th>GMC</th>\n",
       "      <th>...</th>\n",
       "      <th>OLDS</th>\n",
       "      <th>OLDSMOBILE</th>\n",
       "      <th>PLYMOUTH</th>\n",
       "      <th>PONT</th>\n",
       "      <th>PONTIAC</th>\n",
       "      <th>SATURN</th>\n",
       "      <th>SUBARU</th>\n",
       "      <th>TOYOTA</th>\n",
       "      <th>VW</th>\n",
       "      <th>其他制造商</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>是否违约Y</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>85</td>\n",
       "      <td>35</td>\n",
       "      <td>137</td>\n",
       "      <td>208</td>\n",
       "      <td>526</td>\n",
       "      <td>79</td>\n",
       "      <td>411</td>\n",
       "      <td>859</td>\n",
       "      <td>25</td>\n",
       "      <td>109</td>\n",
       "      <td>...</td>\n",
       "      <td>76</td>\n",
       "      <td>27</td>\n",
       "      <td>62</td>\n",
       "      <td>20</td>\n",
       "      <td>168</td>\n",
       "      <td>76</td>\n",
       "      <td>19</td>\n",
       "      <td>339</td>\n",
       "      <td>55</td>\n",
       "      <td>502</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>33</td>\n",
       "      <td>57</td>\n",
       "      <td>128</td>\n",
       "      <td>15</td>\n",
       "      <td>122</td>\n",
       "      <td>253</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "      <td>...</td>\n",
       "      <td>23</td>\n",
       "      <td>5</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "      <td>58</td>\n",
       "      <td>12</td>\n",
       "      <td>6</td>\n",
       "      <td>78</td>\n",
       "      <td>13</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "汽车制造商  BUICK  CADILLAC  CHEV  CHEVROLET  CHEVY  CHRYSLER  DODGE  FORD  GEO  \\\n",
       "是否违约Y                                                                        \n",
       "0         85        35   137        208    526        79    411   859   25   \n",
       "1         17         4    33         57    128        15    122   253    6   \n",
       "\n",
       "汽车制造商  GMC  ...  OLDS  OLDSMOBILE  PLYMOUTH  PONT  PONTIAC  SATURN  SUBARU  \\\n",
       "是否违约Y       ...                                                              \n",
       "0      109  ...    76          27        62    20      168      76      19   \n",
       "1       25  ...    23           5        15     5       58      12       6   \n",
       "\n",
       "汽车制造商  TOYOTA  VW  其他制造商  \n",
       "是否违约Y                     \n",
       "0         339  55    502  \n",
       "1          78  13    121  \n",
       "\n",
       "[2 rows x 32 columns]"
      ]
     },
     "execution_count": 2252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top20 = data2.汽车制造商.value_counts()[data2.汽车制造商.value_counts() > 20].index\n",
    "data2.汽车制造商[~(data2.汽车制造商.isin(top20))] = '其他制造商'\n",
    "cross_tab = pd.crosstab(data2.是否违约Y,data2.汽车制造商)\n",
    "cross_tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2253,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42.08872677057252,\n",
       " 0.08833955925477444,\n",
       " 31,\n",
       " array([[ 81.11137725,  31.01317365, 135.18562874, 210.73053892,\n",
       "         520.06706587,  74.7497006 , 423.84670659, 884.27305389,\n",
       "          24.65149701, 106.55808383, 166.1988024 ,  54.0742515 ,\n",
       "          32.60359281, 158.24670659,  62.82155689,  22.26586826,\n",
       "          61.23113772,  17.49461078,  77.13532934,  18.28982036,\n",
       "          19.88023952, 129.61916168,  78.7257485 ,  25.44670659,\n",
       "          61.23113772,  19.88023952, 179.71736527,  69.97844311,\n",
       "          19.88023952, 331.60239521,  54.0742515 , 495.41556886],\n",
       "        [ 20.88862275,   7.98682635,  34.81437126,  54.26946108,\n",
       "         133.93293413,  19.2502994 , 109.15329341, 227.72694611,\n",
       "           6.34850299,  27.44191617,  42.8011976 ,  13.9257485 ,\n",
       "           8.39640719,  40.75329341,  16.17844311,   5.73413174,\n",
       "          15.76886228,   4.50538922,  19.86467066,   4.71017964,\n",
       "           5.11976048,  33.38083832,  20.2742515 ,   6.55329341,\n",
       "          15.76886228,   5.11976048,  46.28263473,  18.02155689,\n",
       "           5.11976048,  85.39760479,  13.9257485 , 127.58443114]]))"
      ]
     },
     "execution_count": 2253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.chi2_contingency(cross_tab)\n",
    "# alpha = 0.05  P值 = 0.088\n",
    "# 汽车制造商对是否违约无影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2254,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2.drop(['汽车制造商'],inplace=True,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2255,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>是否曾经破产</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "      <th>未填写</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>是否违约Y</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4163</td>\n",
       "      <td>345</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1017</td>\n",
       "      <td>103</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "是否曾经破产     N    Y  未填写\n",
       "是否违约Y                 \n",
       "0       4163  345  140\n",
       "1       1017  103   77"
      ]
     },
     "execution_count": 2255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.是否曾经破产[data2.是否曾经破产.isna()] = '未填写'\n",
    "cross_tab = pd.crosstab(data2.是否违约Y,data2.是否曾经破产)\n",
    "cross_tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2256,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34.01227675032151,\n",
       " 4.114603064282081e-08,\n",
       " 2,\n",
       " array([[4119.18562874,  356.25389222,  172.56047904],\n",
       "        [1060.81437126,   91.74610778,   44.43952096]]))"
      ]
     },
     "execution_count": 2256,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.chi2_contingency(cross_tab)\n",
    "# 是否曾经破产对是否违约有显著影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2257,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "是否违约Y                       0\n",
       "汽车购买时间                      1\n",
       "是否曾经破产                      0\n",
       "五年内信用不良事件数量               213\n",
       "账户数量                      213\n",
       "最久账户存续时间(月)               216\n",
       "在使用账户数量                  1419\n",
       "在使用可循环贷款账户数量(比如信用卡)       638\n",
       "在使用可循环贷款帐户余额(比如信用卡欠款)     478\n",
       "可循环贷款帐户限额(信用卡授权额度)        478\n",
       "可循环贷款帐户使用比例(余额/限额)          0\n",
       "FICO打分                    314\n",
       "汽车购买金额(元)                   0\n",
       "建议售价                        1\n",
       "分期付款的首次交款                   0\n",
       "贷款期限(月)                     0\n",
       "贷款金额                        0\n",
       "贷款金额/建议售价*100               1\n",
       "月均收入(元)                     5\n",
       "行使历程(Mile)                  1\n",
       "是否二手车                       0\n",
       "样本权重                        0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2257,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2258,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SelectPercentile(percentile=70,\n",
       "                 score_func=<function ttest_ind at 0x000001F0B84DBB70>)"
      ]
     },
     "execution_count": 2258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.feature_selection import SelectPercentile\n",
    "feature_select = SelectPercentile(stats.ttest_ind,percentile=70)  # ttest_ind 为双边T检验\n",
    "feature_select.fit(data2.iloc[:,3:].dropna(axis=1),data2.是否违约Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2259,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False,  True,  True, False,  True])"
      ]
     },
     "execution_count": 2259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_select.get_support()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2260,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['可循环贷款帐户使用比例(余额/限额)', '汽车购买金额(元)', '贷款期限(月)', '贷款金额', '样本权重'], dtype='object')"
      ]
     },
     "execution_count": 2260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.iloc[:,3:].dropna(axis=1).columns[feature_select.get_support()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2261,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfff_col = data2.iloc[:,3:].columns.difference(['可循环贷款帐户使用比例(余额/限额)', '汽车购买金额(元)',\n",
    "                                                '贷款期限(月)', '贷款金额', '样本权重'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2262,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 2262,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAUVklEQVR4nO3df2zd9X3v8ec7xiUV4S50uBEQQrrVTIf5qlBZFKnWVGsrgd4rwf5ZCdOaDYtMXLBWaVJh8x/0brKU6G7t2qgXlcpR6TQMlVZo2Ngoi869m6V1w1SMJlgBC2hxgJBdyo+Yxhjnff/w1+GE5oeP7fj46+/zIR2d73mf7/ec90H4db75fL/n843MRJJUDWta3YAkafkY+pJUIYa+JFWIoS9JFWLoS1KFnNPqBk7nwgsvzM2bN7e6DUkqlSeffPI/M7PjZM+t6N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jQj4gNwM8z89WFvobKydCXWucbwLYFbHcucNEiDwLfDHxzEdurpDxlU63wGvCdiDhWPF7D7F7ryWqs1npm/igi6sWPs96KiIcb/ht9iNlQPmn9A/P4TwIDEfH7DbVXT1OH2YPIf40qxwnXJKlCHN6RpAox9CWpQgx9SaoQQ1+SKsTQl6QK+f9zxq2KGB+jFAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data2['可循环贷款帐户使用比例(余额/限额)'].plot.box()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2263,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "157.5"
      ]
     },
     "execution_count": 2263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "up_whisker = data2['可循环贷款帐户使用比例(余额/限额)'].quantile(.75)+\\\n",
    "1.5*(data2['可循环贷款帐户使用比例(余额/限额)'].quantile(.75) - data2['可循环贷款帐户使用比例(余额/限额)'].quantile(.25))\n",
    "up_whisker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2264,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['可循环贷款帐户使用比例(余额/限额)'] = data2['可循环贷款帐户使用比例(余额/限额)'].mask(data2['可循环贷款帐户使用比例(余额/限额)'] \n",
    "                                                               > up_whisker,up_whisker)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2265,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 2265,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVIAAAEvCAYAAAAXaUnwAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWX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SSVMkglaRKBqkkVTJIJamSQSpJlQxSSapkkEpSpbYP0ohYGRH/JyJ2RcSGVvcjSadr6yCNiC7ge8AqYBlwW0Qsa21XknSqtg5S4FpgV2a+mplHgYeB1S3uSZJOcUGrGziHy4E3x73fA3zh9EERsRZYC3DllVd+Op2pbS3e8ERTxu6++8uNtKMO0O5BOimZ+QDwAEBPT0+2uB21mIGnT1u7H9rvBa4Y935RqUlS22j3IH0WWBIRV0XELOBWYFuLe5KkU7T1oX1mHouIdcB2oAt4MDNfanFbknSKtg5SgMx8Eniy1X1I0tm0+6G9JLU9g1SSKhmkklTJIJWkSgapJFUySCWpkkEqSZUi8/y6NT0i9gGvt7oPTQsXA3/Z6iY0bfxyZi4804rzLkilyYqI4czsaXUfmv48tJekSgapJFUySNXJHmh1Azo/eI5Ukiq5RypJlQxSSapkkEqTFBFzIuK/l8eEn23Mwoj40afZl1qv7X/YWZ0rIv4AuA44VkoXAM+cpUYz65n5B8A/BX6QmaMR8Z+AS8e1+1nGnuCwOSLeiogvZub/bOCfrWnIIFW7uzUzDwJExHzg985SO9vYqaz/NvBPyvLhzPzKiSYj4teBXy9vt5axBmmH8NBemoTy8MW/lpm7JzF8GPi7ze1I7cQglSbnYuDgJMe+A1zWvFbUbgxSaXI+BC6c5NgLy3h1CINUmoTMPAB0RcRkwvRXgReb3JLaiEEqTd5TwPWTGLcceKLJvaiNGKTS5H0PWDOJcb8J/GmTe1Eb8fIntbN3gIci4nh5PwP40VlqNLuemT+NiMFyQf67EbF1XK+zgP8YEQuBPyqnAtQh/NESSarkob0kVTJIJamSQSpJlQxSSapkkEpSpf8HInAmMTZCxokAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(5,5))\n",
    "data2['汽车购买金额(元)'].plot.box()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2266,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42224.0"
      ]
     },
     "execution_count": 2266,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "up_whisker = data2['汽车购买金额(元)'].quantile(.75)+\\\n",
    "1.5*(data2['汽车购买金额(元)'].quantile(.75) - data2['汽车购买金额(元)'].quantile(.25))\n",
    "up_whisker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2267,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['汽车购买金额(元)'] = data2['汽车购买金额(元)'].mask(data2['汽车购买金额(元)'] > up_whisker,up_whisker)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2268,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 2268,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAT8AAAEvCAYAAAAzcMYwAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAARPElEQVR4nO3db2ydZ3nH8e9VJ20Qg6alXtQlEe5EBk6NaCurC8KbCBkTLYj0BaBm0RoxS3mxzgIxaWTzGybNUnlDRyNULWBGuhFD1QGNoOqoEneVp7XDoV1batZ6FVUSpY2BtjBYlrRce+E7xemS+tg+p8cn9/cjWed+rud+zrksWT89f48jM5Gk2lzQ7gYkqR0MP0lVMvwkVcnwk1Qlw09SlQw/SVVa0e4GAC677LLs6elpdxuSzjOHDh36cWZ2n23dsgi/np4eJicn292GpPNMRDxzrnUe9kqqkuEnqUqGn6QqGX6SqmT4SaqS4SepSoafpCoZfuoYY2Nj9PX10dXVRV9fH2NjY+1uSR1sWdzkLM1nbGyM4eFhRkdHGRgYYGJigsHBQQC2bdvW5u7UiWI5fJNzf39/+oSHXktfXx+7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djRwLwPA//Y4l7UAbzVRa+X48AdEfGrsnwBcO85aiy0npnfj4jxcpPzzyLiW3M++0Lg7yKiG/hcOUxW5fxiA0lV8rBXUpUMP0lVMvwkVcnwk1Qlw09Slf4Pjyq5eEdV+bQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(5,5))\n",
    "data2['贷款期限(月)'].plot.box()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2269,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "73.5 37.5\n"
     ]
    }
   ],
   "source": [
    "up_whisker = data2['贷款期限(月)'].quantile(.75)+\\\n",
    "1.5*(data2['贷款期限(月)'].quantile(.75) - data2['贷款期限(月)'].quantile(.25))\n",
    "down_whisker = data2['贷款期限(月)'].quantile(.25)-\\\n",
    "1.5*(data2['贷款期限(月)'].quantile(.75) - data2['贷款期限(月)'].quantile(.25))\n",
    "print(up_whisker,down_whisker)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2270,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['贷款期限(月)'] = data2['贷款期限(月)'].mask(data2['贷款期限(月)'] > up_whisker,up_whisker)\n",
    "data2['贷款期限(月)'] = data2['贷款期限(月)'].mask(data2['贷款期限(月)'] < down_whisker,down_whisker)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2271,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 2271,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAD4CAYAAAAZ1BptAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il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qZEhIkioZEtL8eaTuDkiXyjEJSVIlryQkSZUMCUlSJUNCklRpwa+4lhayiPgDYBNwupQWA89X1LiUemb+Qav6Lc2WISE1787MPAkQESuB36+oVbW9UF2qlbebJEmVDAlJUiVDQpJUyZCQJFUyJCRJlQwJSVIlp8BKzXkTeCwipsr+IuDPKmrMoS7Vygf8SZIqebtJklTJkJAkVTIkJEmVDAlJUiVDQpJU6f8D+menYWN88YcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data2['贷款金额'].plot.box()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2272,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "40465.5"
      ]
     },
     "execution_count": 2272,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "up_whisker = data2['贷款金额'].quantile(.75)+\\\n",
    "1.5*(data2['贷款金额'].quantile(.75) - data2['贷款金额'].quantile(.25))\n",
    "up_whisker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2273,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2['贷款金额'] = data2['贷款金额'].mask(data2['贷款金额'] > up_whisker,up_whisker)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 缺失值处理 + 编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2274,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "是否违约Y                    0.000000\n",
       "汽车购买时间                   0.000171\n",
       "是否曾经破产                   0.000000\n",
       "五年内信用不良事件数量              0.036441\n",
       "账户数量                     0.036441\n",
       "最久账户存续时间(月)              0.036955\n",
       "在使用账户数量                  0.242772\n",
       "在使用可循环贷款账户数量(比如信用卡)      0.109153\n",
       "在使用可循环贷款帐户余额(比如信用卡欠款)    0.081779\n",
       "可循环贷款帐户限额(信用卡授权额度)       0.081779\n",
       "可循环贷款帐户使用比例(余额/限额)       0.000000\n",
       "FICO打分                   0.053721\n",
       "汽车购买金额(元)                0.000000\n",
       "建议售价                     0.000171\n",
       "分期付款的首次交款                0.000000\n",
       "贷款期限(月)                  0.000000\n",
       "贷款金额                     0.000000\n",
       "贷款金额/建议售价*100            0.000171\n",
       "月均收入(元)                  0.000855\n",
       "行使历程(Mile)               0.000171\n",
       "是否二手车                    0.000000\n",
       "样本权重                     0.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 2274,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.apply(lambda x:x.isna().sum()/x.size,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2275,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2_code.dropna(axis=0,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2276,
   "metadata": {},
   "outputs": [],
   "source": [
    "dummoes = pd.get_dummies(data2.是否曾经破产,prefix='是否曾经破产')\n",
    "data2_code = pd.concat([data2,dummoes],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2277,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否违约Y</th>\n",
       "      <th>汽车购买时间</th>\n",
       "      <th>五年内信用不良事件数量</th>\n",
       "      <th>账户数量</th>\n",
       "      <th>最久账户存续时间(月)</th>\n",
       "      <th>在使用账户数量</th>\n",
       "      <th>在使用可循环贷款账户数量(比如信用卡)</th>\n",
       "      <th>在使用可循环贷款帐户余额(比如信用卡欠款)</th>\n",
       "      <th>可循环贷款帐户限额(信用卡授权额度)</th>\n",
       "      <th>可循环贷款帐户使用比例(余额/限额)</th>\n",
       "      <th>...</th>\n",
       "      <th>贷款期限(月)</th>\n",
       "      <th>贷款金额</th>\n",
       "      <th>贷款金额/建议售价*100</th>\n",
       "      <th>月均收入(元)</th>\n",
       "      <th>行使历程(Mile)</th>\n",
       "      <th>是否二手车</th>\n",
       "      <th>样本权重</th>\n",
       "      <th>是否曾经破产_N</th>\n",
       "      <th>是否曾经破产_Y</th>\n",
       "      <th>是否曾经破产_未填写</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>506.0</td>\n",
       "      <td>500.0</td>\n",
       "      <td>101.0</td>\n",
       "      <td>...</td>\n",
       "      <td>37.5</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>99.0</td>\n",
       "      <td>6550.00</td>\n",
       "      <td>24000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>34605.0</td>\n",
       "      <td>57241.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>...</td>\n",
       "      <td>60.0</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>99.0</td>\n",
       "      <td>4666.67</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>60.0</td>\n",
       "      <td>10500.00</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2000.00</td>\n",
       "      <td>19600.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4019.0</td>\n",
       "      <td>5946.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>...</td>\n",
       "      <td>60.0</td>\n",
       "      <td>10800.00</td>\n",
       "      <td>118.0</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>104.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1800.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>60.0</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>122.0</td>\n",
       "      <td>4144.00</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5840</th>\n",
       "      <td>0</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>417.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1859.0</td>\n",
       "      <td>52200.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>37.5</td>\n",
       "      <td>31000.00</td>\n",
       "      <td>100.0</td>\n",
       "      <td>5000.00</td>\n",
       "      <td>45000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5841</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4992.0</td>\n",
       "      <td>5066.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>...</td>\n",
       "      <td>60.0</td>\n",
       "      <td>24970.00</td>\n",
       "      <td>117.0</td>\n",
       "      <td>2400.00</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5842</th>\n",
       "      <td>0</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>972.0</td>\n",
       "      <td>5616.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>...</td>\n",
       "      <td>37.5</td>\n",
       "      <td>20949.00</td>\n",
       "      <td>113.0</td>\n",
       "      <td>1837.50</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5843</th>\n",
       "      <td>0</td>\n",
       "      <td>1999.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>13714.0</td>\n",
       "      <td>14061.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>...</td>\n",
       "      <td>48.0</td>\n",
       "      <td>17100.00</td>\n",
       "      <td>60.0</td>\n",
       "      <td>28000.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5844</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3767.0</td>\n",
       "      <td>23080.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>...</td>\n",
       "      <td>52.0</td>\n",
       "      <td>19609.90</td>\n",
       "      <td>97.0</td>\n",
       "      <td>2700.00</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5845 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      是否违约Y  汽车购买时间  五年内信用不良事件数量  账户数量  最久账户存续时间(月)  在使用账户数量  \\\n",
       "0         1  1998.0          7.0   9.0         64.0      2.0   \n",
       "1         0  2000.0          0.0  21.0        240.0     11.0   \n",
       "2         1  1998.0          7.0  10.0         60.0      NaN   \n",
       "3         1  1997.0          3.0  10.0         35.0      5.0   \n",
       "4         0  2000.0          0.0  10.0        104.0      2.0   \n",
       "...     ...     ...          ...   ...          ...      ...   \n",
       "5840      0  1997.0          0.0  21.0        417.0      4.0   \n",
       "5841      0  2000.0          2.0   8.0         62.0      5.0   \n",
       "5842      0  1997.0          0.0   6.0         30.0      4.0   \n",
       "5843      0  1999.0          0.0   9.0         67.0      7.0   \n",
       "5844      0  2000.0          1.0  34.0        130.0      8.0   \n",
       "\n",
       "      在使用可循环贷款账户数量(比如信用卡)  在使用可循环贷款帐户余额(比如信用卡欠款)  可循环贷款帐户限额(信用卡授权额度)  \\\n",
       "0                     1.0                  506.0               500.0   \n",
       "1                     7.0                34605.0             57241.0   \n",
       "2                     NaN                    NaN                 NaN   \n",
       "3                     4.0                 4019.0              5946.0   \n",
       "4                     0.0                    0.0              1800.0   \n",
       "...                   ...                    ...                 ...   \n",
       "5840                  2.0                 1859.0             52200.0   \n",
       "5841                  3.0                 4992.0              5066.0   \n",
       "5842                  3.0                  972.0              5616.0   \n",
       "5843                  5.0                13714.0             14061.0   \n",
       "5844                  2.0                 3767.0             23080.0   \n",
       "\n",
       "      可循环贷款帐户使用比例(余额/限额)  ...  贷款期限(月)      贷款金额  贷款金额/建议售价*100   月均收入(元)  \\\n",
       "0                  101.0  ...     37.5  17200.00           99.0   6550.00   \n",
       "1                   60.0  ...     60.0  19588.54           99.0   4666.67   \n",
       "2                    0.0  ...     60.0  10500.00           92.0   2000.00   \n",
       "3                   68.0  ...     60.0  10800.00          118.0   1500.00   \n",
       "4                    0.0  ...     60.0  26328.04          122.0   4144.00   \n",
       "...                  ...  ...      ...       ...            ...       ...   \n",
       "5840                 4.0  ...     37.5  31000.00          100.0   5000.00   \n",
       "5841                99.0  ...     60.0  24970.00          117.0   2400.00   \n",
       "5842                17.0  ...     37.5  20949.00          113.0   1837.50   \n",
       "5843                98.0  ...     48.0  17100.00           60.0  28000.00   \n",
       "5844                16.0  ...     52.0  19609.90           97.0   2700.00   \n",
       "\n",
       "      行使历程(Mile)  是否二手车  样本权重  是否曾经破产_N  是否曾经破产_Y  是否曾经破产_未填写  \n",
       "0        24000.0      1  1.00         1         0           0  \n",
       "1           22.0      0  4.75         1         0           0  \n",
       "2        19600.0      1  1.00         1         0           0  \n",
       "3        10000.0      1  1.00         1         0           0  \n",
       "4           14.0      0  4.75         1         0           0  \n",
       "...          ...    ...   ...       ...       ...         ...  \n",
       "5840     45000.0      1  4.75         1         0           0  \n",
       "5841        21.0      0  4.75         0         1           0  \n",
       "5842     25000.0      1  4.75         1         0           0  \n",
       "5843         0.0      0  4.75         1         0           0  \n",
       "5844        12.0      0  4.75         1         0           0  \n",
       "\n",
       "[5845 rows x 24 columns]"
      ]
     },
     "execution_count": 2277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_code.drop(['是否曾经破产'],axis = 1,inplace = True)\n",
    "data2_code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 衍生字段每个4分（至少5个共20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2278,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 贷款收入比\n",
    "data2_code['贷款_收入'] = data2_code.贷款金额/data2_code['月均收入(元)']\n",
    "# 月均收入(元)与汽车购买金额(元)比\n",
    "data2_code['收入_购买金额'] = data2_code['月均收入(元)']/data2_code['汽车购买金额(元)']\n",
    "# 贷款金额与汽车购买金额(元)比\n",
    "data2_code['贷款_购买金额'] = data2_code['贷款金额']/data2_code['汽车购买金额(元)']\n",
    "# 分期付款的首次交款与汽车购买金额(元)比\n",
    "data2_code['分期首交款_购买金额'] = data2_code['分期付款的首次交款']/data2_code['汽车购买金额(元)']\n",
    "# 存款与收入比\n",
    "# data.存款/data.收入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2279,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2_code.drop(data2_code[dfff_col],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2280,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否违约Y</th>\n",
       "      <th>汽车购买时间</th>\n",
       "      <th>可循环贷款帐户使用比例(余额/限额)</th>\n",
       "      <th>汽车购买金额(元)</th>\n",
       "      <th>贷款期限(月)</th>\n",
       "      <th>贷款金额</th>\n",
       "      <th>样本权重</th>\n",
       "      <th>是否曾经破产_N</th>\n",
       "      <th>是否曾经破产_Y</th>\n",
       "      <th>是否曾经破产_未填写</th>\n",
       "      <th>贷款_收入</th>\n",
       "      <th>收入_购买金额</th>\n",
       "      <th>贷款_购买金额</th>\n",
       "      <th>分期首交款_购买金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>101.0</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>37.5</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.625954</td>\n",
       "      <td>0.380814</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>60.0</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.197541</td>\n",
       "      <td>0.238235</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.034895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13595.00</td>\n",
       "      <td>60.0</td>\n",
       "      <td>10500.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5.250000</td>\n",
       "      <td>0.147113</td>\n",
       "      <td>0.772343</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>12999.00</td>\n",
       "      <td>60.0</td>\n",
       "      <td>10800.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.200000</td>\n",
       "      <td>0.115393</td>\n",
       "      <td>0.830833</td>\n",
       "      <td>0.238403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>60.0</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6.353292</td>\n",
       "      <td>0.157399</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5840</th>\n",
       "      <td>0</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>37.5</td>\n",
       "      <td>31000.00</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6.200000</td>\n",
       "      <td>inf</td>\n",
       "      <td>inf</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5841</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>99.0</td>\n",
       "      <td>24970.00</td>\n",
       "      <td>60.0</td>\n",
       "      <td>24970.00</td>\n",
       "      <td>4.75</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>10.404167</td>\n",
       "      <td>0.096115</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5842</th>\n",
       "      <td>0</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>20949.00</td>\n",
       "      <td>37.5</td>\n",
       "      <td>20949.00</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11.400816</td>\n",
       "      <td>0.087713</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5843</th>\n",
       "      <td>0</td>\n",
       "      <td>1999.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>22400.00</td>\n",
       "      <td>48.0</td>\n",
       "      <td>17100.00</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.610714</td>\n",
       "      <td>1.250000</td>\n",
       "      <td>0.763393</td>\n",
       "      <td>0.236607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5844</th>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>19609.90</td>\n",
       "      <td>52.0</td>\n",
       "      <td>19609.90</td>\n",
       "      <td>4.75</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.262926</td>\n",
       "      <td>0.137686</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5845 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      是否违约Y  汽车购买时间  可循环贷款帐户使用比例(余额/限额)  汽车购买金额(元)  贷款期限(月)      贷款金额  样本权重  \\\n",
       "0         1  1998.0               101.0   17200.00     37.5  17200.00  1.00   \n",
       "1         0  2000.0                60.0   19588.54     60.0  19588.54  4.75   \n",
       "2         1  1998.0                 0.0   13595.00     60.0  10500.00  1.00   \n",
       "3         1  1997.0                68.0   12999.00     60.0  10800.00  1.00   \n",
       "4         0  2000.0                 0.0   26328.04     60.0  26328.04  4.75   \n",
       "...     ...     ...                 ...        ...      ...       ...   ...   \n",
       "5840      0  1997.0                 4.0       0.00     37.5  31000.00  4.75   \n",
       "5841      0  2000.0                99.0   24970.00     60.0  24970.00  4.75   \n",
       "5842      0  1997.0                17.0   20949.00     37.5  20949.00  4.75   \n",
       "5843      0  1999.0                98.0   22400.00     48.0  17100.00  4.75   \n",
       "5844      0  2000.0                16.0   19609.90     52.0  19609.90  4.75   \n",
       "\n",
       "      是否曾经破产_N  是否曾经破产_Y  是否曾经破产_未填写      贷款_收入   收入_购买金额   贷款_购买金额  \\\n",
       "0            1         0           0   2.625954  0.380814  1.000000   \n",
       "1            1         0           0   4.197541  0.238235  1.000000   \n",
       "2            1         0           0   5.250000  0.147113  0.772343   \n",
       "3            1         0           0   7.200000  0.115393  0.830833   \n",
       "4            1         0           0   6.353292  0.157399  1.000000   \n",
       "...        ...       ...         ...        ...       ...       ...   \n",
       "5840         1         0           0   6.200000       inf       inf   \n",
       "5841         0         1           0  10.404167  0.096115  1.000000   \n",
       "5842         1         0           0  11.400816  0.087713  1.000000   \n",
       "5843         1         0           0   0.610714  1.250000  0.763393   \n",
       "5844         1         0           0   7.262926  0.137686  1.000000   \n",
       "\n",
       "      分期首交款_购买金额  \n",
       "0       0.000000  \n",
       "1       0.034895  \n",
       "2       0.000000  \n",
       "3       0.238403  \n",
       "4       0.000000  \n",
       "...          ...  \n",
       "5840         NaN  \n",
       "5841    0.000000  \n",
       "5842    0.000000  \n",
       "5843    0.236607  \n",
       "5844    0.000000  \n",
       "\n",
       "[5845 rows x 14 columns]"
      ]
     },
     "execution_count": 2280,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2281,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "是否违约Y                 0.000000\n",
       "汽车购买时间                0.000171\n",
       "可循环贷款帐户使用比例(余额/限额)    0.000000\n",
       "汽车购买金额(元)             0.000000\n",
       "贷款期限(月)               0.000000\n",
       "贷款金额                  0.000000\n",
       "样本权重                  0.000000\n",
       "是否曾经破产_N              0.000000\n",
       "是否曾经破产_Y              0.000000\n",
       "是否曾经破产_未填写            0.000000\n",
       "贷款_收入                 0.000855\n",
       "收入_购买金额               0.001198\n",
       "贷款_购买金额               0.000000\n",
       "分期首交款_购买金额            0.006843\n",
       "dtype: float64"
      ]
     },
     "execution_count": 2281,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_code.apply(lambda x:x.isna().sum()/x.size,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2282,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2_code.贷款_收入[data2_code.贷款_收入 == np.inf] = 0\n",
    "data2_code.贷款_收入[data2_code.贷款_收入.isna()] = 0\n",
    "data2_code.收入_购买金额 [data2_code.收入_购买金额 == np.inf] = 0\n",
    "data2_code.收入_购买金额[data2_code.收入_购买金额.isna()] = 0\n",
    "data2_code.贷款_购买金额[data2_code.贷款_购买金额 == np.inf] = 0\n",
    "data2_code.贷款_购买金额[data2_code.贷款_购买金额.isna()] = 0\n",
    "data2_code.分期首交款_购买金额[data2_code.分期首交款_购买金额 == np.inf] = 0\n",
    "data2_code.分期首交款_购买金额[data2_code.分期首交款_购买金额.isna()] = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " # 失衡数据判断并处理 10分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2283,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4648\n",
       "1    1197\n",
       "Name: 是否违约Y, dtype: int64"
      ]
     },
     "execution_count": 2283,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_code.是否违约Y.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2284,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.20479041916167665"
      ]
     },
     "execution_count": 2284,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1197/data2_code.是否违约Y.size\n",
    "# 大于10%不属于失衡数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2285,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "是否违约Y                 0\n",
       "汽车购买时间                1\n",
       "可循环贷款帐户使用比例(余额/限额)    0\n",
       "汽车购买金额(元)             0\n",
       "贷款期限(月)               0\n",
       "贷款金额                  0\n",
       "样本权重                  0\n",
       "是否曾经破产_N              0\n",
       "是否曾经破产_Y              0\n",
       "是否曾经破产_未填写            0\n",
       "贷款_收入                 0\n",
       "收入_购买金额               0\n",
       "贷款_购买金额               0\n",
       "分期首交款_购买金额            0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2285,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_code.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2286,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2_code.dropna(axis=0,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多个备选模型比较20分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2293,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "xtrain,xtest,ytrain,ytest = train_test_split(data2_code.iloc[:,1:],data2_code[['是否违约Y']],test_size = \n",
    "                                             0.3,random_state = 420)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2394,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.svm import SVC\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score, classification_report\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2399,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(xtrain, ytrain, xtest, ytest, model_name,model):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    #创建指定模型\n",
    "    clf=model \n",
    "    start = time.time()\n",
    "    \n",
    "    #训练模型\n",
    "    clf.fit(xtrain, ytrain.values.ravel())  # values.ravel()表示可将多维数组变成一维数组\n",
    "    \n",
    "    #验证模型\n",
    "    print(\"训练集评估\")\n",
    "    train_pre = clf.predict(xtrain) \n",
    "    print(classification_report(ytrain,train_pre))\n",
    "    \n",
    "    print(\"检验集评估\")\n",
    "    test_pre=clf.predict(xtest)\n",
    "    print(classification_report(ytest,test_pre))\n",
    "\n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "\n",
    "    return clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2400,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      1.00      0.89      3259\n",
      "           1       0.82      0.07      0.13       831\n",
      "\n",
      "    accuracy                           0.81      4090\n",
      "   macro avg       0.81      0.53      0.51      4090\n",
      "weighted avg       0.81      0.81      0.74      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.80      1.00      0.89      1388\n",
      "           1       0.73      0.04      0.08       366\n",
      "\n",
      "    accuracy                           0.80      1754\n",
      "   macro avg       0.76      0.52      0.48      1754\n",
      "weighted avg       0.78      0.80      0.72      1754\n",
      "\n",
      "模型训练耗时：0.104578s\n",
      "训练DT\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      3259\n",
      "           1       1.00      1.00      1.00       831\n",
      "\n",
      "    accuracy                           1.00      4090\n",
      "   macro avg       1.00      1.00      1.00      4090\n",
      "weighted avg       1.00      1.00      1.00      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      1388\n",
      "           1       1.00      1.00      1.00       366\n",
      "\n",
      "    accuracy                           1.00      1754\n",
      "   macro avg       1.00      1.00      1.00      1754\n",
      "weighted avg       1.00      1.00      1.00      1754\n",
      "\n",
      "模型训练耗时：0.019946s\n",
      "训练AdaBoost\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      3259\n",
      "           1       1.00      1.00      1.00       831\n",
      "\n",
      "    accuracy                           1.00      4090\n",
      "   macro avg       1.00      1.00      1.00      4090\n",
      "weighted avg       1.00      1.00      1.00      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      1388\n",
      "           1       1.00      1.00      1.00       366\n",
      "\n",
      "    accuracy                           1.00      1754\n",
      "   macro avg       1.00      1.00      1.00      1754\n",
      "weighted avg       1.00      1.00      1.00      1754\n",
      "\n",
      "模型训练耗时：0.021942s\n",
      "训练GBDT\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      3259\n",
      "           1       1.00      1.00      1.00       831\n",
      "\n",
      "    accuracy                           1.00      4090\n",
      "   macro avg       1.00      1.00      1.00      4090\n",
      "weighted avg       1.00      1.00      1.00      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      1388\n",
      "           1       1.00      1.00      1.00       366\n",
      "\n",
      "    accuracy                           1.00      1754\n",
      "   macro avg       1.00      1.00      1.00      1754\n",
      "weighted avg       1.00      1.00      1.00      1754\n",
      "\n",
      "模型训练耗时：0.353089s\n",
      "训练RF\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      3259\n",
      "           1       1.00      1.00      1.00       831\n",
      "\n",
      "    accuracy                           1.00      4090\n",
      "   macro avg       1.00      1.00      1.00      4090\n",
      "weighted avg       1.00      1.00      1.00      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      1388\n",
      "           1       1.00      1.00      1.00       366\n",
      "\n",
      "    accuracy                           1.00      1754\n",
      "   macro avg       1.00      1.00      1.00      1754\n",
      "weighted avg       1.00      1.00      1.00      1754\n",
      "\n",
      "模型训练耗时：0.384964s\n",
      "训练XGBoost\n",
      "[15:20:21] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.0/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      3259\n",
      "           1       1.00      1.00      1.00       831\n",
      "\n",
      "    accuracy                           1.00      4090\n",
      "   macro avg       1.00      1.00      1.00      4090\n",
      "weighted avg       1.00      1.00      1.00      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      1388\n",
      "           1       1.00      1.00      1.00       366\n",
      "\n",
      "    accuracy                           1.00      1754\n",
      "   macro avg       1.00      1.00      1.00      1754\n",
      "weighted avg       1.00      1.00      1.00      1754\n",
      "\n",
      "模型训练耗时：0.478920s\n"
     ]
    }
   ],
   "source": [
    "model_name_param_dict = { 'LR': (LogisticRegression()),\n",
    "                          'DT': (DecisionTreeClassifier()),\n",
    "                          'AdaBoost': (AdaBoostClassifier()),\n",
    "                          'GBDT': (GradientBoostingClassifier()),\n",
    "                          'RF': (RandomForestClassifier()),\n",
    "                          'XGBoost':(XGBClassifier())\n",
    "                         }\n",
    "result = {}\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    result[model_name] = train_model(xtrain, ytrain, xtest, ytest, model_name,model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 利用网格搜索调优20分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2401,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'algorithm': 'SAMME', 'base_estimator': DecisionTreeClassifier(max_depth=11, max_features=13), 'n_estimators': 1} 1.0\n"
     ]
    }
   ],
   "source": [
    "# AdaBoost模型得分高，用时少\n",
    "param_grid={\n",
    "    'base_estimator':[DecisionTreeClassifier(max_depth=11,max_features=13)],\n",
    "    'n_estimators':range(1,300,10),\n",
    "    'algorithm': ['SAMME','SAMME.R']\n",
    "    \n",
    "}\n",
    "model = AdaBoostClassifier()\n",
    "grid_search = GridSearchCV(model,param_grid=param_grid,cv=5,scoring='roc_auc')\n",
    "temp = grid_search.fit(xtrain,ytrain)\n",
    "print(temp.best_params_,temp.best_score_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2402,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['explained_variance', 'r2', 'max_error', 'neg_median_absolute_error', 'neg_mean_absolute_error', 'neg_mean_absolute_percentage_error', 'neg_mean_squared_error', 'neg_mean_squared_log_error', 'neg_root_mean_squared_error', 'neg_mean_poisson_deviance', 'neg_mean_gamma_deviance', 'accuracy', 'top_k_accuracy', 'roc_auc', 'roc_auc_ovr', 'roc_auc_ovo', 'roc_auc_ovr_weighted', 'roc_auc_ovo_weighted', 'balanced_accuracy', 'average_precision', 'neg_log_loss', 'neg_brier_score', 'adjusted_rand_score', 'rand_score', 'homogeneity_score', 'completeness_score', 'v_measure_score', 'mutual_info_score', 'adjusted_mutual_info_score', 'normalized_mutual_info_score', 'fowlkes_mallows_score', 'precision', 'precision_macro', 'precision_micro', 'precision_samples', 'precision_weighted', 'recall', 'recall_macro', 'recall_micro', 'recall_samples', 'recall_weighted', 'f1', 'f1_macro', 'f1_micro', 'f1_samples', 'f1_weighted', 'jaccard', 'jaccard_macro', 'jaccard_micro', 'jaccard_samples', 'jaccard_weighted'])"
      ]
     },
     "execution_count": 2402,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sklearn\n",
    "sklearn.metrics.SCORERS.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2403,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0 max_depth : 11 max_features : 13\n",
      "score : 0.5 max_depth : 1 max_features : 1\n",
      "score : 0.5 max_depth : 1 max_features : 2\n",
      "score : 0.6 max_depth : 1 max_features : 3\n",
      "score : 0.8 max_depth : 1 max_features : 4\n",
      "score : 0.8 max_depth : 1 max_features : 5\n",
      "score : 0.8 max_depth : 1 max_features : 6\n",
      "score : 0.8 max_depth : 1 max_features : 6\n",
      "score : 0.9 max_depth : 1 max_features : 8\n",
      "score : 1.0 max_depth : 1 max_features : 9\n",
      "score : 1.0 max_depth : 1 max_features : 9\n",
      "score : 1.0 max_depth : 1 max_features : 11\n",
      "score : 1.0 max_depth : 1 max_features : 11\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 1 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 10\n",
      "score : 1.0 max_depth : 2 max_features : 11\n",
      "score : 1.0 max_depth : 2 max_features : 12\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 2 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 10\n",
      "score : 1.0 max_depth : 3 max_features : 10\n",
      "score : 1.0 max_depth : 3 max_features : 12\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 3 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 7\n",
      "score : 1.0 max_depth : 4 max_features : 7\n",
      "score : 1.0 max_depth : 4 max_features : 7\n",
      "score : 1.0 max_depth : 4 max_features : 10\n",
      "score : 1.0 max_depth : 4 max_features : 11\n",
      "score : 1.0 max_depth : 4 max_features : 12\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 4 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 5\n",
      "score : 1.0 max_depth : 5 max_features : 5\n",
      "score : 1.0 max_depth : 5 max_features : 5\n",
      "score : 1.0 max_depth : 5 max_features : 8\n",
      "score : 1.0 max_depth : 5 max_features : 9\n",
      "score : 1.0 max_depth : 5 max_features : 10\n",
      "score : 1.0 max_depth : 5 max_features : 11\n",
      "score : 1.0 max_depth : 5 max_features : 12\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 5 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 7\n",
      "score : 1.0 max_depth : 6 max_features : 7\n",
      "score : 1.0 max_depth : 6 max_features : 9\n",
      "score : 1.0 max_depth : 6 max_features : 10\n",
      "score : 1.0 max_depth : 6 max_features : 11\n",
      "score : 1.0 max_depth : 6 max_features : 12\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 6 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 7\n",
      "score : 1.0 max_depth : 7 max_features : 7\n",
      "score : 1.0 max_depth : 7 max_features : 9\n",
      "score : 1.0 max_depth : 7 max_features : 9\n",
      "score : 1.0 max_depth : 7 max_features : 11\n",
      "score : 1.0 max_depth : 7 max_features : 12\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 7 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 7\n",
      "score : 1.0 max_depth : 8 max_features : 8\n",
      "score : 1.0 max_depth : 8 max_features : 9\n",
      "score : 1.0 max_depth : 8 max_features : 10\n",
      "score : 1.0 max_depth : 8 max_features : 11\n",
      "score : 1.0 max_depth : 8 max_features : 12\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 8 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 6\n",
      "score : 1.0 max_depth : 9 max_features : 6\n",
      "score : 1.0 max_depth : 9 max_features : 8\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score : 1.0 max_depth : 9 max_features : 9\n",
      "score : 1.0 max_depth : 9 max_features : 10\n",
      "score : 1.0 max_depth : 9 max_features : 11\n",
      "score : 1.0 max_depth : 9 max_features : 12\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 9 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 6\n",
      "score : 1.0 max_depth : 10 max_features : 6\n",
      "score : 1.0 max_depth : 10 max_features : 6\n",
      "score : 1.0 max_depth : 10 max_features : 9\n",
      "score : 1.0 max_depth : 10 max_features : 10\n",
      "score : 1.0 max_depth : 10 max_features : 11\n",
      "score : 1.0 max_depth : 10 max_features : 12\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 10 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 7\n",
      "score : 1.0 max_depth : 11 max_features : 8\n",
      "score : 1.0 max_depth : 11 max_features : 9\n",
      "score : 1.0 max_depth : 11 max_features : 10\n",
      "score : 1.0 max_depth : 11 max_features : 11\n",
      "score : 1.0 max_depth : 11 max_features : 12\n",
      "score : 1.0 max_depth : 11 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 13\n",
      "score : 1.0 max_depth : 11 max_features : 13\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "n_estimators2 = 1\n",
    "score = 0\n",
    "for i in range(12):\n",
    "    for j in range(20):\n",
    "        AD = AdaBoostClassifier(DecisionTreeClassifier(max_depth=i,max_features=j),n_estimators=n_estimators2,algorithm='SAMME')\n",
    "        cross_val_mean = cross_val_score(AD,xtrain,ytrain,cv=5,scoring='roc_auc').mean()\n",
    "        if cross_val_mean >=score:\n",
    "            score = cross_val_mean\n",
    "            tree_depth = i\n",
    "            tree_feature = j\n",
    "        print('score :',score,'max_depth :',tree_depth,'max_features :',tree_feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2404,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练AdaBoostClassifier\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      3259\n",
      "           1       1.00      1.00      1.00       831\n",
      "\n",
      "    accuracy                           1.00      4090\n",
      "   macro avg       1.00      1.00      1.00      4090\n",
      "weighted avg       1.00      1.00      1.00      4090\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00      1388\n",
      "           1       1.00      1.00      1.00       366\n",
      "\n",
      "    accuracy                           1.00      1754\n",
      "   macro avg       1.00      1.00      1.00      1754\n",
      "weighted avg       1.00      1.00      1.00      1754\n",
      "\n",
      "模型训练耗时：0.023935s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "AdaBoostClassifier(algorithm='SAMME',\n",
       "                   base_estimator=DecisionTreeClassifier(max_depth=11,\n",
       "                                                         max_features=13),\n",
       "                   n_estimators=1)"
      ]
     },
     "execution_count": 2404,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = AdaBoostClassifier(DecisionTreeClassifier(max_depth=11,max_features=13),\n",
    "                   n_estimators = 1,\n",
    "                   algorithm = 'SAMME')\n",
    "train_model(xtrain=xtrain,ytrain=ytrain,xtest=xtest,ytest=ytest,model_name='AdaBoostClassifier',model=model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 优质模型保存5分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2395,
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2411,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AdaBoostClassifier(algorithm='SAMME',\n",
       "                   base_estimator=DecisionTreeClassifier(max_depth=11,\n",
       "                                                         max_features=13),\n",
       "                   n_estimators=1)"
      ]
     },
     "execution_count": 2411,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据处理的保存：\n",
    "# 是否曾经破产：编码--独热--3列\n",
    "\n",
    "from sklearn.externals import joblib\n",
    "# 保存模型\n",
    "joblib.dump(model,'model.model')\n",
    "\n",
    "# 加载模型\n",
    "clf = joblib.load('model.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2407,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.22044605e-16, 1.00000000e+00],\n",
       "       [2.22044605e-16, 1.00000000e+00],\n",
       "       [2.22044605e-16, 1.00000000e+00],\n",
       "       ...,\n",
       "       [1.00000000e+00, 2.22044605e-16],\n",
       "       [1.00000000e+00, 2.22044605e-16],\n",
       "       [1.00000000e+00, 2.22044605e-16]])"
      ]
     },
     "execution_count": 2407,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['AdaBoost'].predict_proba(xtest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
